/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.google.common.collect.Lists;
import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.DateUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpBuyPlan;
import com.jims.master.modules.csm.entity.ExpInfo;
import com.jims.master.modules.csm.entity.ExpProvideApplication;
import com.jims.master.modules.csm.entity.ExpSupplierCata;
import com.jims.master.modules.csm.service.ExpBuyPlanService;
import com.jims.master.modules.csm.service.ExpSupplierCataService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * 消耗品采购计划
 * @author DT
 * @version 2017-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/buyExpPlan")
public class ExpBuyPlanCreateController extends BaseController {

	@Autowired
	private ExpBuyPlanService expBuyPlanService;
	@ModelAttribute
	public ExpBuyPlan get(@RequestParam(required=false) String id) {
		ExpBuyPlan entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = expBuyPlanService.get(id);
		}
		if (entity == null){
			entity = new ExpBuyPlan();
		}
		return entity;
	}

    /**
     * 采购计划生成首页
     * @param expBuyPlan
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"createPlanIndex",""})
    public String index(ExpBuyPlan expBuyPlan,HttpServletRequest request,HttpServletResponse response,Model model){
        String start = DateUtils.getDate() + " 00:00:00";
        String stop = DateUtils.getDate() + " 23:59:59";
        Date startDate = DateUtils.parseDate(start);
        Date stopDate = DateUtils.parseDate(stop);
        if(null == expBuyPlan.getStartDate()){
            expBuyPlan.setStartDate(startDate);
        }
        if(null == expBuyPlan.getStopDate()){
            expBuyPlan.setStopDate(stopDate);
        }
        Page<ExpBuyPlan> page = expBuyPlanService.findPage(new Page<ExpBuyPlan>(request, response), expBuyPlan);
        model.addAttribute("expBuyPlan",expBuyPlan);
        model.addAttribute("page", page);
        return "modules/csm/buyExpPlanCreate";
    }

    /**
     * 保存采购计划(生成)
     * @param expBuyPlan
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveExpBuyPlanCreate")
    @ResponseBody
    public StringData saveExpBuyPlanCreate(ExpBuyPlan expBuyPlan,Model model){
        StringData data = new StringData();
        int num = expBuyPlanService.saveExpBuyPlanCreate(expBuyPlan);
        if (num > 0) {
            data.setCode("success");
            data.setData("保存成功");
        } else {
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
    }

    /**
     * 采购对消入出库
     * @param expBuyPlan
     * @param request
     * @param response
     * @param model
     * @return
     * @author fengyg
     * @date 2017-06-28
     */
    @RequiresPermissions("user")
    @RequestMapping(value = {"buyPlanImpExpIndex", ""})
    public String buyPlanImpExp(ExpBuyPlan expBuyPlan, HttpServletRequest request, HttpServletResponse response, Model model) {
        List<ExpBuyPlan> list = Lists.newArrayList();
        Double totalMoney = 0.00;
        if (StringUtils.isNotBlank(expBuyPlan.getPurchaseNumber())){
             list = expBuyPlanService.findExpProvideApplicationByNumber(expBuyPlan.getPurchaseNumber());
            if(null != list && list.size() > 0){
                totalMoney = list.get(0).getTotalMoney();
            }
        }
        model.addAttribute("totalMoney",totalMoney);
        model.addAttribute("list", list);
        return "modules/csm/buyPlanImpExp";
    }

    /**
     * 保存采购对消入出库
     * @param expBuyPlan
     * @return
     * @author fengyg
     * @date 2017-07-21
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveBuyPlanImpExp")
    @ResponseBody
    public StringData saveBuyPlanImpExp(ExpBuyPlan expBuyPlan){
        StringData data = new StringData();
        int num = expBuyPlanService.saveBuyPlanImpExp(expBuyPlan);
        if(num > 0){
            data.setCode("success");
            data.setData("采购对消入出库保存成功!");
        }else if(num == -1){
            data.setCode("error");
            data.setData("没有数据需要保存!");
        }else if(num == -2){
            data.setCode("error");
            data.setData("批号不能为空!");
        }else{
            data.setCode("error");
            data.setData("保存失败!");
        }
        return data;
    }

    /**
     * 根据采购单号获取采购单详细信息(采购入出库页面提取需要入出库的数据)
     * @param purchaseNumber 采购单号
     * @return
     * @author fengyg
     * @date 2017-07-18
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "getImpExpList")
    public String getImpExpList(@RequestParam("purchaseNumber")String purchaseNumber,Model model){
        ExpBuyPlan expBuyPlan = new ExpBuyPlan();
        expBuyPlan.setPurchaseNumber(purchaseNumber);
        List<ExpBuyPlan> list = expBuyPlanService.findByNumber(expBuyPlan);
        Page<ExpBuyPlan> page = new Page<>();
        page.setList(list);
        model.addAttribute("page", page);
        return "modules/csm/buyPlanImpExp";
    }

    /**
     * 采购对消入出库，修改某一行申请数据页面
     * @return
     * @author fengyg
     * @date 2017-07-20
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "updateForm")
    public String updateForm(ExpBuyPlan expBuyPlan,String idxIndex,HttpServletRequest request,HttpServletResponse response,Model model){
        ExpBuyPlan returnBuyPlan = expBuyPlanService.getByApplicationId(expBuyPlan);
        model.addAttribute("idxIndex",idxIndex);
        model.addAttribute("expBuyPlan",returnBuyPlan);
        return "modules/csm/buyPlanImpExpForm";
    }

	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(HttpServletRequest request,HttpServletResponse response,@RequestParam("id")String id) {
	    StringData data=new StringData();
        ExpBuyPlan expBuyPlan = new ExpBuyPlan();
        expBuyPlan.setId(id);
		int i=expBuyPlanService.delete(expBuyPlan);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}

}